package com.gs.action;

import java.util.Iterator;
import java.util.List;

import javax.annotation.Resource;

import org.apache.struts2.json.annotations.JSON;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.gs.action.datamodel.ModuelDateModel;
import com.gs.bean.Moduel;
import com.gs.dao.ModuleDAO;
import com.liang.log.Log;

/**
 * <font color="red"><b>
 * 
 * @author pjt </b></font>
 */
@SuppressWarnings("serial")
@Scope(value = "prototype")
@Controller(value = "moduleAction")
public class ModuleAction extends BasicAction{

	@Resource
	private ModuleDAO moduleDAOImpl;

	private Integer pid;
	private Integer id;

	private List<Moduel> modules;

	public String loadByPid() throws Exception {

		System.out.println("loadbyPid");
		List<Moduel> modules = moduleDAOImpl.findModules(pid);

		Iterator<Moduel> ite = modules.iterator();

		String result = "";
		Moduel module = null;
		while (ite.hasNext()) {
			module = ite.next();
			result += ",{text:'" + module.getName() + "',id:'" + module.getId();
			if (module.getUrl() == null) {
				result += "',leaf:false";
			} else {
				result += "',jsurl:'" + module.getJsUrl() + "',leaf:true,action:'"+module.getUrl()+"'";
			}
			result += "}";
		}
		result = '['+result.substring(1)+']';
		Log.info("A Module execute:" + result);
		
		writeString(result);
		return null;
	}

	@Override
	public String execute() throws Exception {

		System.out.println("execute" + pid);
		try {
			ModuelDateModel searchModel = new ModuelDateModel();
			// searchModule.setId(1);
			modules = moduleDAOImpl.queryPage(searchModel,
					searchModel.getCurrentPage(), searchModel.getPageSize())
					.getList();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "SYS.MANAGER.MODULE";
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public void setPid(Integer pid) {
		this.pid = pid;
	}

	@JSON
	public List<Moduel> getModules() {
		return modules;
	}

	public void setModules(List<Moduel> modules) {
		this.modules = modules;
	}

}
